<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="站点名称">
              <a-input v-model="queryParam.dto.siteName" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="所属组织">
              <a-tree-select
                v-model="queryParam.dto.orgId"
                :treeData="treeData"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                placeholder="请选择所属组织"
                allowClear
                treeDefaultExpandAll
              ></a-tree-select>
              <!-- <a-input v-model="queryParam.dto.siteSubtitle" placeholder="" /> -->
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.dto.siteKeyWords" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" v-action:site:save @click="$refs.formHandle.add()">新建站点</a-button>
    </div>
    <s-table
      ref="table"
      size="small"
      bordered
      rowKey="id"
      :columns="columns"
      :data="loadData">
      <span slot="action" slot-scope="text, record">
        <template>
          <a-tag color="orange" v-action:site:edit @click="handleEdit(record)" >编辑</a-tag>
          <a-tag color="red" v-action:site:del @click="handleDelete(record)" >删除</a-tag>
          <a-tag color="green" v-action:site:chose:temp @click="chooseTemplate(record)" >选择模板</a-tag>
          <!-- <a-dropdown>
            <a-menu slot="overlay">
              <a-tag color="green" v-action:menu:del @click="setDefault(record)" >设为组织默认</a-tag>
            </a-menu>
            <a> <a-tag color="green" >更多 <a-icon type="down"/></a-tag></a>
          </a-dropdown> -->

        </template>
      </span>

      <span slot="isDefault" slot-scope="text">
        <template>
          <a-icon type="check" v-if="text" />
          <a-icon type="close" v-else/>
        </template>
      </span>

      <span slot="siteLogo" slot-scope="row,text">
        <template>
          <img style="width: 30px;height: 30px;" :src="JSON.parse(row).url" v-if="row"/>
        </template>
      </span>

      <span slot="isShareOrg" slot-scope="text">
        <template>
          <a-icon type="check" v-if="text" />
          <a-icon type="close" v-else/>
        </template>
      </span>

    </s-table>
    <form-handle ref="formHandle" @ok="handleOk" />
    <templates ref="templates"></templates>
  </a-card>
</template>

<script>
  import formHandle from './handle'
  import templates from './templates'
  // import { setSite } from '@/utils/request'
  import {
    page,
    delByPk,
    setOrgDefault,
    pageOrg
  } from '@/api/site/site'
  import {
    STable
  } from '@/components'
  export default {
    name: 'TableList',
    components: {
      STable,
      formHandle,
      templates
    },
    props: {
      statusFilter: {
        type: Function,
        default: undefined
      }
    },
    data () {
      return {
        mdl: {},
        advanced: false,
        treeData: [],
        queryParam: {
          dto: {}
        },
        columns: [{
            title: '站点名称',
            dataIndex: 'siteName'
          },
          {
            title: '关键字',
            dataIndex: 'siteKeyWords'
          },
          {
            title: '站点LOGO',
            dataIndex: 'siteLogo',
            scopedSlots: {
              customRender: 'siteLogo'
            }
          },
          {
            title: '域名',
            dataIndex: 'domain'
          },
          {
            title: '是否默认',
            dataIndex: 'isDefault',
            scopedSlots: {
              customRender: 'isDefault'
            }
          },
          {
            title: '是否共享',
            dataIndex: 'isShareOrg',
            scopedSlots: {
              customRender: 'isShareOrg'
            }
          },

          {
            title: '创建时间',
            dataIndex: 'gmtCreate'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then(response => {
            return response.res
          })
        }
      }
    },
    created () { this.loadTreeData() },
    methods: {
      handleDelete (record) {
        const that = this
        this.$confirm({
          title: '操作提醒',
          content: '确定要删除吗?',
          onOk () {
            delByPk({
              id: record.id
            }).then(response => {
              if (response.code === 0) {
                that.$message.info(response.msg)
                that.handleOk()
              } else {
                that.$message.error(response.msg)
              }
            }).catch((response) => {
              that.$message.warn(response.msg)
            })
          },
          onCancel () {}
        })
      },
      loadTreeData () {
        pageOrg({}).then(response => {
          this.treeData = response.res.children
        })
      },
      setDefault (record) {
        const that = this
        this.$confirm({
          title: '操作提醒',
          content: '确定要设置默认站点吗?',
          onOk () {
            setOrgDefault({
              siteId: record.id
            }).then(response => {
              if (response.code === 0) {
                // setSite(record.id)
                that.$message.info(response.msg)
                that.handleOk()
                that.$router.push({ path: '/' })
                window.location.reload()
              }
            }).catch((response) => {
              that.$message.warn(response.msg)
            })
          },
          onCancel () {}
        })
      },
      chooseTemplate (record) {
        this.$refs.templates.showDrawer(record)
      },
      handleEdit (record) {
        this.$refs.formHandle.edit(record)
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      resetSearchForm () {
        this.queryParam = {
          dto: {}
        }
        this.handleOk()
      }
    }
  }
</script>
